<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: create_account.php 495 2014-01-30 16:38:01Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 495 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-30 16:38:01 +0000 (Thu, 30 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
 define('GLOBAL_COLUMN_LEFT', '0');
include('includes/application_top.php');

if(isset($_SESSION['customer_id']))
	redirect(href_link(FILENAME_ACCOUNT, '', 'SSL'));

$smarty = new Smarty();

require(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');

require_once(DIR_FS_INC.'inc.validate_email.php');
require_once(DIR_FS_INC.'inc.encrypt_password.php');
require_once(DIR_FS_INC.'inc.get_geo_zone_code.php');
require_once(DIR_FS_INC.'inc.write_user_info.php');
require(DIR_WS_CLASSES.'class.input_fields.php');

$field = new input_fields('create_account', $_POST);

$gender_male = $field->getField('gender', 'm', (!isset($_POST) ? ' checked=""' : ''));
$gender_female = $field->getField('gender', 'f');
$title = $field->getField('title', '', 'size="6"');
$firstname = $field->getField('firstname');
$lastname = $field->getField('lastname');
$dob = $field->getField('dob');
$email_address = $field->getField('email');
$email_confirmation = $field->getField('email_confirmation');
$company = $field->getField('company');
$vat = $field->getField('vat');
$street_address = $field->getField('street_address', '', 'size="21" class="create_account_street"');
$street_address_num = $field->getField('street_address_num', '', 'size="3" class="create_account_street_num"');
$suburb = $field->getField('suburb');
$postcode = $field->getField('postcode', '', 'size="5"  class="create_account_postcode"');
$city = $field->getField('city', '', 'size="19" class="create_account_city"');
$zone_id = $field->getField('state', '', 'class="create_account_state"');
$country = $field->getField('country', '', 'class="create_account_state" id="country"');
$telephone = $field->getField('telephone');
$fax = $field->getField('fax');
$newsletter = $field->getField('newsletter', '1');
$password = $field->getField('password');
$confirmation = $field->getField('confirmation');
$dsg = $field->getField('dsg', '1');

$process = false;
if (isset($_POST['action']) && ($_POST['action'] == 'process') && $field->error != 'true') {
	$process = true;
		
	if ($field->customers_status == 0 || !$field->customers_status)
		$field->customers_status = DEFAULT_CUSTOMERS_STATUS_ID;

	if($field->error == 'false') {
		$sql_data_array = array('customers_vat_id' => $field->post_vars['vat'],
								'customers_vat_id_status' => $field->customers_vat_id_status,
								'customers_status' => $field->customers_status,
								'customers_firstname' => (!empty($field->post_vars['title']) ? $field->post_vars['title'].' ' : '' ).ucfirst(strtolower($field->post_vars['firstname'])),
								'customers_lastname' => ucfirst(strtolower($field->post_vars['lastname'])),
								'customers_email_address' => $field->post_vars['email'],
								'customers_telephone' => $field->post_vars['telephone'],
								'customers_fax' => $field->post_vars['fax'],
								'customers_password' => encrypt_password($field->post_vars['password']),
								'customers_date_added' => 'NOW()',
								'customers_last_modified' => 'NOW()',
								'datensg' => 'NOW()');

		if (!empty($field->post_vars['gender']))
			$sql_data_array['customers_gender'] = $field->post_vars['gender'];
		if (!empty($field->post_vars['dob']))
			$sql_data_array['customers_dob'] = date_raw($field->post_vars['dob']);
		
		require(DIR_FS_INC.'inc.new_customer_id.php');
		$sql_data_array['customers_cid'] = new_customer_id();

		$db->db_perform(TABLE_CUSTOMERS, $sql_data_array);

		$_SESSION['customer_id'] = $db->db_insert_id();
		$user_id = $db->db_insert_id();
		
		write_user_info($user_id);
		
		$sql_data_array = array('customers_id' => $_SESSION['customer_id'],
								'entry_firstname' => (!empty($field->post_vars['title']) ? $field->post_vars['title'].' ' : '' ).ucfirst(strtolower($field->post_vars['firstname'])),
								'entry_lastname' => ucfirst(strtolower($field->post_vars['lastname'])),
								'entry_street_address' => $field->post_vars['street_address'].' '.$field->post_vars['street_address_num'],
								'entry_postcode' => $field->post_vars['postcode'],
								'entry_city' => $field->post_vars['city'],
								'entry_country_id' => $field->post_vars['country'],
								'entry_state' => $field->post_vars['state'],
								'entry_suburb' => $field->post_vars['suburb'],
								'address_date_added' => 'NOW()',
								'address_last_modified' => 'NOW()');

		if (!empty($field->post_vars['gender']))
			$sql_data_array['entry_gender'] = $field->post_vars['gender'];
		if (!empty($field->post_vars['company']))
			$sql_data_array['entry_company'] = $field->post_vars['company'];

		$db->db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);

		$address_id = $db->db_insert_id();

		$db->db_query("UPDATE 
							".TABLE_CUSTOMERS." 
						SET 
							customers_default_address_id = '".$address_id."' 
						WHERE 
							customers_id = '".(int)$_SESSION['customer_id']."'");

		$db->db_query("INSERT INTO 
							".TABLE_CUSTOMERS_INFO." 
							(customers_info_id, 
							customers_info_number_of_logons, 
							customers_info_date_account_created) 
						VALUES 
							('".(int)$_SESSION['customer_id']."', 
							'0', 
							NOW())");

		if (SESSION_RECREATE == 'True')
			session_recreate();

		if($field->post_vars['newsletter'] == '1') {

			require_once(DIR_FS_INC.'inc.random_charcode.php');
			$vlcode = random_charcode(32);

			$sql_newletter_array = array('customers_email_address' => $field->post_vars['email'],
										'customers_id' => (int)$_SESSION['customer_id'],
										'customers_status' => $field->customers_status,
										'customers_firstname' => $field->post_vars['firstname'],
										'customers_lastname' => $field->post_vars['lastname'],
										'mail_status' => '0',
										'mail_key' => $vlcode,
										'date_added' => 'NOW()');

			$db->db_perform(TABLE_NEWSLETTER_RECIPIENTS, $sql_newletter_array);
			
			$link = href_link(FILENAME_NEWSLETTER, 'action=activate&email='.$field->post_vars['email'].'&key='.$vlcode, 'NONSSL');

			$smarty->assign('EMAIL', $field->post_vars['email']);
			$smarty->assign('LINK', $link);

			$smarty->assign('language', $_SESSION['language']);
			$smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
			$smarty->assign('logo_path', HTTP_SERVER.DIR_WS_CATALOG.DIR_WS_IMAGES);

			$smarty->caching = false;
			require_once (DIR_FS_INC.'inc.get_mail_body.php');
			$html_mail = $smarty->fetch('html:newsletter_aktivierung');
			$html_mail .= $signatur_html;
			$txt_mail = $smarty->fetch('txt:newsletter_aktivierung');
			$txt_mail .= $signatur_text;
			require_once (DIR_FS_INC.'inc.get_mail_data.php');
			$mail_data = get_mail_data('newsletter_aktivierung');

			$newsletter_subject = str_replace('{$shop_besitzer}', STORE_OWNER, $mail_data['EMAIL_SUBJECT']);
			$newsletter_subject = str_replace('{$shop_name}', STORE_NAME, $newsletter_subject);

			$newsletter_from = str_replace('{$shop_name}', STORE_NAME, $mail_data['EMAIL_ADDRESS_NAME']);

			if (SEND_EMAILS == true) {
				php_mail($mail_data['EMAIL_ADDRESS'],
						$newsletter_from,
						$field->post_vars['email'],
						$field->post_vars['firstname'] .' '. $field->post_vars['lastname'],
						$mail_data['EMAIL_FORWARD'],
						$mail_data['EMAIL_REPLAY_ADDRESS'],
						$mail_data['EMAIL_REPLAY_ADDRESS_NAME'],
						'',
						'',
						$newsletter_subject,
						$html_mail,
						$txt_mail);
			}
		}

		$_SESSION['customer_first_name'] = (!empty($field->post_vars['title']) ? $field->post_vars['title'].' ' : '' ).$field->post_vars['firstname'];
		$_SESSION['customer_last_name'] = $field->post_vars['lastname'];
		$_SESSION['customer_default_address_id'] = $address_id;
		$_SESSION['customer_country_id'] = $field->post_vars['country'];
		$_SESSION['customer_zone_id'] = $field->post_vars['zone_id'];
		$_SESSION['customer_vat_id'] = $field->post_vars['vat'];

		$_SESSION['cart']->restore_contents();

		$smarty->assign('language', $_SESSION['language']);
		
		if(!empty($field->post_vars['gender']))
			$smarty->assign('GENDER',$field->post_vars['gender']);

		require_once (DIR_FS_INC.'inc.get_mail_data.php');
		$mail_data = get_mail_data('create_account');
		$smarty->assign('MAIL_REPLY_ADDRESS', $mail_data['EMAIL_ADDRESS']);
		$smarty->assign('VNAME', $_SESSION['customer_first_name']);
		$smarty->assign('NNAME', $_SESSION['customer_last_name']);
		$smarty->assign('logo_path', HTTP_SERVER.DIR_WS_CATALOG.DIR_WS_IMAGES);
		$smarty->assign('content', $module_content);
		$smarty->caching = false;
		$smarty->assign('USERNAME4MAIL', $field->post_vars['email']);

		if (isset ($_SESSION['tracking']['refID'])){
			$campaign = $db->db_query("SELECT * FROM ".TABLE_CAMPAIGNS." WHERE campaigns_refID = '".$_SESSION[tracking][refID]."'");
			
			if ($campaign->_numOfRows)
				$refID = $campaign->fields['campaigns_id'];
			else
				$refID = 0;

			$db->db_query("UPDATE ".TABLE_CUSTOMERS." SET refferers_id = '".$refID."' WHERE customers_id = '".(int)$_SESSION['customer_id']."'");

			$leads = $campaign->fields['campaigns_leads'] + 1 ;
			$db->db_query("UPDATE ".TABLE_CAMPAIGNS." SET campaigns_leads = '".$leads."' WHERE campaigns_id = '".$refID."'");
		}

		if (ACTIVATE_GIFT_SYSTEM == 'true') {
			if (NEW_SIGNUP_GIFT_VOUCHER_AMOUNT > 0) {
				$coupon_code = create_coupon_code();
				$insert_query = $db->db_query("INSERT INTO ".TABLE_COUPONS." (coupon_code, coupon_type, coupon_amount, date_created) VALUES ('".$coupon_code."', 'G', '".NEW_SIGNUP_GIFT_VOUCHER_AMOUNT."', now())");
				$insert_id = $db->db_insert_id();
				$insert_query = $db->db_query("INSERT INTO ".TABLE_COUPON_EMAIL_TRACK." (coupon_id, customer_id_sent, sent_firstname, emailed_to, date_sent) VALUES ('".$insert_id."', '0', 'Admin', '".$field->post_vars['email']."', NOW() )");

				$smarty->assign('SEND_GIFT', 'true');
				$smarty->assign('GIFT_AMMOUNT', $price->format(NEW_SIGNUP_GIFT_VOUCHER_AMOUNT, true));
				$smarty->assign('GIFT_CODE', $coupon_code);
				$smarty->assign('GIFT_LINK', href_link(FILENAME_GV_REDEEM, 'gv_no='.$coupon_code, 'NONSSL', false));

			}
			if (NEW_SIGNUP_DISCOUNT_COUPON != '') {
				$coupon_code = NEW_SIGNUP_DISCOUNT_COUPON;

				$coupon = $db->db_query("SELECT * from ".TABLE_COUPONS." WHERE coupon_code = '".$coupon_code."'");
				$smarty->assign('COUPON_CODE', $coupon->fields['coupon_code']);

				$coupon_desc = $db->db_query("SELECT * from ".TABLE_COUPONS_DESCRIPTION." WHERE coupon_id = '".$coupon->fields['coupon_id']."' and language_id = '".(int)$_SESSION['languages_id']."'");
				$smarty->assign('COUPON_DESC', $coupon_desc->fields['coupon_description']);

				$insert_query = $db->db_query("INSERT INTO ".TABLE_COUPON_EMAIL_TRACK." (coupon_id, customer_id_sent, sent_firstname, emailed_to, date_sent) VALUES ('".$coupon->fields['coupon_id']."', '0', 'Admin', '".$field->post_vars['email']."', NOW() )");

				$smarty->assign('SEND_COUPON', 'true');
			}
		}
		require_once (DIR_FS_INC.'inc.get_mail_body.php');
		$smarty->caching = false;
		$html_mail = $smarty->fetch('html:create_account');
		$html_mail .= $signatur_html;
		$smarty->caching = false;
		$txt_mail = $smarty->fetch('txt:create_account');
		$txt_mail .= $signatur_text;
		require_once (DIR_FS_INC.'inc.get_mail_data.php');
		$mail_data = get_mail_data('create_account');

		php_mail($mail_data['EMAIL_ADDRESS'],
					$mail_data['EMAIL_ADDRESS_NAME'],
					$field->post_vars['email'],
					$name,
					$mail_data['EMAIL_FORWARD'],
					$mail_data['EMAIL_REPLAY_ADDRESS'],
					$mail_data['EMAIL_REPLAY_ADDRESS_NAME'],
					'',
					'',
					$mail_data['EMAIL_SUBJECT'],
					$html_mail,
					$txt_mail);

		if(!isset($mail_error)) {
			if($_SESSION['cart']->count_contents() > 0)
				redirect(href_link(FILENAME_SHOPPING_CART, '', 'SSL'));
			else
				redirect(href_link(FILENAME_DEFAULT, '', 'SSL'));
		} else
			echo $mail_error;
	}

} else {

	$breadcrumb->add(NAVBAR_TITLE_CREATE_ACCOUNT, href_link(FILENAME_CREATE_ACCOUNT, '', 'SSL'));

	require (DIR_WS_INCLUDES.'header.php');

	if($field->error == 'true')
		$smarty->assign('error', ENTRY_EMPTY_ERROR);

	$smarty->assign('FORM_ACTION', draw_form('create_account', href_link(FILENAME_CREATE_ACCOUNT, '', 'SSL'), 'post', 'onsubmit="return check_form(create_account);" autocomplete="off"').draw_hidden_field('action', 'process'));
	$smarty->assign('INPUT_TITLE', $title);
	$smarty->assign('INPUT_MALE', $gender_male);
	$smarty->assign('INPUT_FEMALE', $gender_female);
	$smarty->assign('INPUT_FIRSTNAME', $firstname);
	$smarty->assign('INPUT_LASTNAME', $lastname);
	$smarty->assign('INPUT_DOB', $dob);
	$smarty->assign('INPUT_EMAIL', $email_address);
	$smarty->assign('INPUT_EMAIL_CONFIRMATION', $email_confirmation);
	$smarty->assign('INPUT_COMPANY', $company);
	$smarty->assign('INPUT_VAT', $vat);
	$smarty->assign('INPUT_STREET', $street_address);
	$smarty->assign('INPUT_STREET_NUM', $street_address_num);
	$smarty->assign('INPUT_SUBURB', $suburb);
	$smarty->assign('INPUT_CODE', $postcode);
	$smarty->assign('INPUT_CITY', $city);
	$smarty->assign('INPUT_STATE', $zone_id);
	$smarty->assign('INPUT_COUNTRY', $country);
	$smarty->assign('INPUT_TEL', $telephone);
	$smarty->assign('INPUT_FAX', $fax);
	$smarty->assign('INPUT_PASSWORD', $password);
	$smarty->assign('INPUT_CONFIRMATION', $confirmation);
	$smarty->assign('INPUT_NEWSLETTER', $newsletter);

	$shop_content_data = $db->db_query("SELECT content_title,content_heading,content_text,content_file FROM ".TABLE_CONTENT_MANAGER." WHERE content_group='2' AND languages_id='".$_SESSION['languages_id']."'");

  $dsg_error = (strpos($datensg, 'error_border') !== false);
	if ($shop_content_data->fields['content_file'] != '') {
		if($shop_content_data->fields['content_file'] == 'janolaw_datenschutz.php') {
			include(DIR_FS_INC.'inc.janolaw.php');
			$datensg = JanolawContent('datenschutzerklaerung', 'txt');
		} else {
			//$datensg = '<iframe src="'.DIR_WS_CATALOG.'media/content/'.$shop_content_data->fields['content_file'].'" width="100%" height="300"></iframe>';
      ob_start();
      include (DIR_FS_CATALOG.'media/content/'.$shop_content_data->fields['content_file']);
      $content_body = utf8_encode(ob_get_contents());
  		ob_end_clean();
  		$datensg = '<div class="'.($dsg_error?'error_border ':'').'div_textarea">'.$content_body.'</div>';
		}
	} else
		$datensg = '<div class="div_textarea">'.$shop_content_data->fields['content_text'].'</div>';

	$smarty->assign('DSG', $datensg);
	if(SITE_OVERLAYS == 'true') {
		$smarty->assign('BUTTON_PRINT', '<a class="shipping" title="" href="'.href_link(FILENAME_PRINT_CONTENT, 'coID=2&iframe=true').'">'.PRINT_CONTENT.'</a>');
		$smarty->assign('TITLE_DSG', sprintf(INFO_DSG_OVERLAY, href_link(FILENAME_POPUP_CONTENT, 'coID=2&iframe=true')));

	} else {
		$smarty->assign('BUTTON_PRINT', '<a onclick="javascript:window.open(\''.href_link(FILENAME_PRINT_CONTENT, 'coID=2').'\', \'popup\', \'toolbar=0, width=640, height=600\')">'.PRINT_CONTENT.'</a>');
		$smarty->assign('TITLE_DSG', sprintf(INFO_DSG_OVERLAY, href_link(FILENAME_POPUP_CONTENT, 'coID=2&iframe=true')));
	}
	$smarty->assign('DATENSG_CHECKBOX', $dsg);

	$smarty->assign('FORM_END', '</form>');
	$smarty->assign('language', $_SESSION['language']);
	$smarty->caching = false;
	$smarty->assign('BUTTON_SUBMIT', image_submit('button_send.gif', IMAGE_BUTTON_CONTINUE));
	$main_content = $smarty->fetch(CURRENT_TEMPLATE.'/module/create_account.html');

	$smarty->assign('language', $_SESSION['language']);
	$smarty->assign('main_content', $main_content);
	$smarty->caching = false;
	if (!defined('RM'))
		$smarty->loadFilter('output', 'note');
$smarty->loadFilter('output','trimwhitespace');
	$smarty->display(CURRENT_TEMPLATE.'/index.html');
	include ('includes/application_bottom.php');
}
?>